package com.yk.spark.controller.analysis;

import com.yk.spark.model.AppModel;
import com.yk.spark.model.FileModel;
import com.yk.spark.service.analysis.api.IAnalysisService;
import com.yk.spark.service.file.api.IFileService;
import com.yk.spark.service.manage.app.api.IAppService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;

/**
 * Created by Limit on 2017/8/23.
 */
@Controller
@RequestMapping("/analysis")
public class AnalysisController {

    @Resource(name = "analysisService")
    private IAnalysisService analysisService;

    @Resource(name = "appService")
    private IAppService appService;

    @RequestMapping("getAnalysisData")
    @ResponseBody
    Object getAnalysisData(@RequestParam (name = "appId") String appId,@RequestParam(name = "limit") int limit) throws Exception{
        AppModel model=this.appService.getAppModel(appId);
        return this.analysisService.getAnalysisData(model,limit);
    }

    @RequestMapping("exportToExcel")
    @ResponseBody
    void exportToExcel(@RequestParam (name = "appId") String appId, HttpServletResponse response) throws Exception{
        AppModel model=this.appService.getAppModel(appId);
        response.setHeader("content-type", "text/html;charset=UTF-8");
        response.setHeader("content-disposition", "attachment;filename="+ URLEncoder.encode(model.getAppName()+".xlsx","utf-8"));
        this.analysisService.exportToExcel(model,response.getOutputStream());
    }
}
